import 'package:dart_iztro/crape_myrtle/translations/translation_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:forme/screens/other_screen.dart';
import 'package:forme/services/api_service.dart';
import 'package:forme/services/database_service.dart';
import 'package:intl/date_symbol_data_file.dart';
import 'package:intl/intl.dart';
import 'screens/home_screen.dart';

void main() async {
  IztroTranslationService.init(initialLocale: 'zh_CN');
  IztroTranslationService.addAppTranslations({
    'zh_CN': {
      'app_title': '紫微斗数示例应用',
      'birth_info': '出生信息',
      'year': '年',
      'month': '月',
      'day': '日',
      'hour': '时',
      'minute': '分',
      'gender': '性别',
      'male': '男',
      'female': '女',
      'date_type': '日期类型:',
      'solar': '阳历',
      'lunar': '农历',
      'calculate_bazi': '计算八字',
      'calculate_chart': '计算紫微星盘',
      'bazi_result': '八字结果',
      'chart_result': '紫微星盘结果',
      'detail_info': '详细信息:',
      'platform': '运行平台',
    },
    'en_US': {
      'app_title': 'ZiWei Dou Shu Demo',
      'birth_info': 'Birth Information',
      'year': 'Year',
      'month': 'Month',
      'day': 'Day',
      'hour': 'Hour',
      'minute': 'Minute',
      'gender': 'Gender',
      'male': 'Male',
      'female': 'Female',
      'date_type': 'Date Type:',
      'solar': 'Solar',
      'lunar': 'Lunar',
      'calculate_bazi': 'Calculate BaZi',
      'calculate_chart': 'Calculate ZiWei Chart',
      'bazi_result': 'BaZi Result',
      'chart_result': 'ZiWei Chart Result',
      'detail_info': 'Details:',
      'platform': 'Platform',
    },
  });
  final apiService = ApiService();
  final token = await apiService.getAccessToken();
  if (token != null) {
    checkAndSyncOnStart(); // 启动时同步
  }
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    systemNavigationBarColor: Colors.white, // 导航栏背景色
    systemNavigationBarIconBrightness: Brightness.dark, // 导航栏图标颜色（dark为黑色）
  ));
  Intl.defaultLocale = 'zh_CN';
  runApp(const MyApp());
}

Future<void> checkAndSyncOnStart() async {
  final databaseService = DatabaseService();
  await databaseService.syncDatabase(); // 启动时检查并同步数据库
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    const HomeScreen(),
    const OtherScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('zh', 'CN'),
        Locale('en', 'US'),
      ],
      title: 'For me',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: Scaffold(
        body: _pages[_currentIndex],
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
          items: const [
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: '主页',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.list),
              label: '其他',
            ),
          ],
        ),
      ),
    );
  }
}
